package com.sp.homework;

import org.junit.jupiter.api.Test;

import java.io.*;
import java.util.Properties;

/**
 * 1. 要编写一个dog.properties
 * name=tom
 * age=5
 * color=red
 *
 * 2. 编写Dog类(name, age, color), 创建一个dog对象,
 *    读取dog.properties用相应的内容完成属性初始化, 并输出
 *
 * 3. 将创建的Dog对象, 序列化到文件dog.dat文件
 */
public class Homework03 {
    public static void main(String[] args) throws IOException {
        Properties properties = new Properties();
        properties.load(new FileReader("chapter19_IO\\src\\dog.properties"));
        String name = properties.getProperty("name");
        int age = Integer.parseInt(properties.getProperty("age"));
        String color = properties.getProperty("color");
        Dog dog = new Dog(name, age, color);
        System.out.println(dog);


        //序列化
        String serialPath = "d:\\development\\code\\IO_Test\\news\\homework03.txt";
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(serialPath));
        objectOutputStream.writeObject(dog);
        objectOutputStream.close();

    }

    //反序列化查看
    @Test
    public void method() throws IOException, ClassNotFoundException {
        String serialPath = "d:\\development\\code\\IO_Test\\news\\homework03.txt";
        ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(serialPath));
        Object o = objectInputStream.readObject();
        System.out.println(o);
        objectInputStream.close();
    }

}

class Dog implements Serializable {
    private String name;
    private int age;
    private String color;

    public Dog() {
    }

    public Dog(String name, int age, String color) {
        this.name = name;
        this.age = age;
        this.color = color;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", color='" + color + '\'' +
                '}';
    }
}